#include "AudioStream.h"


AudioStream::AudioStream(StreamType type)
{
	this->state = paNoError;
	this->type	= type;
	this->callback = NULL;
}


AudioStream::~AudioStream(void)
{
}

int				AudioStream::Open(IAudio *audio, void *config, void *callback = NULL, void *data = NULL)
{
	PaStreamParameters	*input = NULL, *output = NULL;

	if (!audio->IsInitialized())
		return this->state; // erreur a modifier
	if (config){
		this->config = reinterpret_cast<PaStreamParameters*>(config);
		if (this->type == _INPUT_)
			input = this->config;
		else if (this->type == _OUTPUT_)
			output = this->config;
	}
	if (callback)
		this->callback = reinterpret_cast<PaStreamCallback*>(callback);
	
	this->state = Pa_OpenStream(
						&(this->stream),
						input,
						output,
						SAMPLE_RATE,
						FRAMES_PER_BUFFER*NUM_CHANNELS,
						paClipOff|paDitherOff,
						this->callback,
						data // LES DATAS
	);
	return this->state;
}

int		AudioStream::Close()
{
	if (!this->stream)
		return this->state; // not initialized

	if(!(this->state = Pa_CloseStream(this->stream) != paNoError))
		return this->state; // erreur
	else
		this->stream = NULL;
	return this->state;
}

int		AudioStream::Start()
{
	if (!this->stream)
		return this->state; // not initialized
	if(!(this->state = Pa_StartStream(this->stream) != paNoError))
		return this->state; // erreur

	return this->state;
}

int		AudioStream::Stop()
{
	if (!this->stream)
		return this->state; // not initialized
	if(!(this->state = Pa_StopStream(stream)) != paNoError)
		return this->state; // erreur

	return this->state;
}

int		AudioStream::IsActive()
{
	this->state = Pa_IsStreamActive(this->stream);
	return	this->state;
}